package com.snail.billing.session;

import org.apache.http.NameValuePair;

import com.snail.billing.DataCache;
import com.snail.billing.net.BillingSecurity;
import com.snail.billing.net.BillingTreeArrayHttpSession;
import com.snail.billing.util.BillingUtil;

public class PaymentSession extends BillingTreeArrayHttpSession {

	public PaymentSession() {
		DataCache cache = DataCache.getInstance();
		String host = cache.importParams.hostImprest;
		String address = String.format("http://%s/api/payment/json", host);
		setAddress(address);
		addBillingPair("orderno", DataCache.getInstance().importParams.orderNo);
		addBillingPair("gameid", DataCache.getInstance().importParams.gameId);
		addBillingPair("account", DataCache.getInstance().importParams.account);
		addBillingPair("platformid", String.valueOf(DataCache.getInstance().paymentParams.platformId));
		addBillingPair("imprestmode", "1");
		addBillingPair("money", DataCache.getInstance().importParams.price);
		addBillingPair("clientip", BillingUtil.getLocalIp());
		addBillingPair("productname", DataCache.getInstance().importParams.produceName);
		addBillingPair("backendurl", DataCache.getInstance().importParams.backUrl);
		addBillingPair("fontendurl", DataCache.getInstance().importParams.fontUrl);
		addBillingPair("ext", DataCache.getInstance().importParams.extra);
		buildParamPair();
	}

	@Override
	public void buildParamPair() {
		BillingSecurity security = getSecurity();
		addBillingPair("accessid", security.accessId);
		addBillingPair("accesstype", security.accessType);
		for (NameValuePair pair : getBillingPairList()) {
			addParamPair(pair.getName(), pair.getValue());
		}
		addParamPair("sign", buildSign(security, getBillingPairList()));
	}

	@Override
	public BillingSecurity getSecurity() {
		if (DataCache.getInstance().isSandbox) {
			return BillingSecurity.securitySandbox3;
		}
		return BillingSecurity.security;
	}

}
